import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { NextFunction, Request, Response } from 'express';
import { join } from 'path';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './common/filter/http-exception/http-exception.filter';
import { TransformInterceptor } from './common/interceptor/transform/transform.interceptor';

function MiddleWareAll(req: Request, res: Response, next: NextFunction) {
  console.log(req.originalUrl);
  //可配置白名单  路由拦截  鉴权
  if (['', '2'].includes(req.originalUrl)) {
    next()

  } else {
    next()
  }

}
async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, { cors: true });
  //配置静态资源访问目录
  app.useStaticAssets(join(__dirname,'../../../images')
  ,
  {
    prefix:"/images"//配置访问路径
  }
  )
  //token鉴权 通过中间件实现


  
  //请求拦截
  app.use(MiddleWareAll)
  //响应过滤器
  app.useGlobalInterceptors(new TransformInterceptor());
  //全局异常过滤器
  app.useGlobalFilters(new HttpExceptionFilter());
  app.enableCors();
  await app.listen(3012);
}
bootstrap();
